/**
A multi shaped mine sweeper game
SDLHexamine  Copyright (C) 2009-2010  Andematunum-Software
(andematunum-software@googlegroups.com)

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _resources_h_
#define _resources_h_

#include <stdlib.h>
#include "SDL.h"
#include <map>
#include <memory>
using namespace std;

struct SDL_Surface;

#define TILE_W 48
#define TILE_H 42
#define XOFFSET 36 // Value to add to X for the next column
#define YOFFSET 21 // Value to add to Y for the next row

/**
  * Overload the default implementation of auto_ptr
  * so that it correctly handles surface destruction.
  */
namespace std {

template<>
auto_ptr<SDL_Surface>::~auto_ptr () {
  SDL_FreeSurface(get());
}

}

//typedef auto_ptr<SDL_Surface> auto_SDL_Surface;

class Resources {
  public:
    static Resources* get_instance () {
      if (instance_ == NULL) {
        instance_ = new Resources;
      }
      return instance_;
    }
    static void kill () {
      delete instance_;
      instance_ = NULL;
    }

    /**
      * Get a resource given a key string.
      */
    SDL_Surface* get (const char* key) {
      auto_ptr<SDL_Surface>& psurf = surfaces_.at(key);
      return psurf.get();
    }

    void test ();

  private:
    void load_all_ ();
    void free_all_ ();

    Resources () {
      load_all_ ();
    }

    ~Resources () {
      free_all_ ();
    }

  private:
    map<const char*, auto_ptr<SDL_Surface> > surfaces_;
    static Resources* instance_;
};

#endif

